package connectionchecker;

import clientservice.keyreader;
import clientservice.service;
import plugin.PluginInterface;

public class Main implements PluginInterface {

    private static String[][] groups; // [x][y]  x - group number y - parameter(0 - user, 1 - adress, 2 - port);

    public boolean hasParameters() {
        return true;
    }

    public String[] ParameterNames() {

        return null;
    }

    public String[] GroupNames() {
        String[] names = {"group"};
        return names;

    }

    public String[][] GroupParameterNames() {
        String[][] groupparameters = new String[1][3];
        groupparameters[0][0] = "user";
        groupparameters[0][1] = "adress";
        groupparameters[0][2] = "port";
        return groupparameters;
    }

    public String About() {
        return "Nothing  i can tell you";
    }

    public boolean Crashed() {
        return false;
    }

    public String CrashError() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void Start() {
        Connect();
    }

    public void Stop() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void InputParameters(String[][] Plugin_String_parameter, String[][][][] Plugin_Group_String_parameter) {
        groups = new String[Plugin_Group_String_parameter[0].length][3];
        for (int i = 0; i < Plugin_Group_String_parameter[0].length; i++) {
            groups[i][0] = Plugin_Group_String_parameter[0][i][0][0];
            groups[i][1] = Plugin_Group_String_parameter[0][i][1][0];
            groups[i][2] = Plugin_Group_String_parameter[0][i][2][0];
        }
    }

    private void Connect() {
        for (int i = 0; i < groups.length; i++) {
            String user = groups[i][0];
            String address = groups[i][1];
            int port = Integer.parseInt(groups[i][2]);

            if (new service().Connect(address, port)) {
                System.out.println("connected");
                if (new service().LogIn(user, new keyreader().read(user))) {
                    System.out.println("logged");

                } else {
                    System.out.println("access deny");

                }
                new service().CloseConnection();
            } else {
                System.out.println("connection failed");
            }
        }
    }
}
